// 权限控制
// 放行的页面
async function checkLogin() {
  console.log('auth.js:'+location.pathname)
  // 修复：添加userToken变量定义
  const userToken = localStorage.getItem('token');
  if (userToken == null || userToken == '') {
    console.log('auth.js:登录状态(null token):' + false)
    return null
  }
  let formData = new FormData()
  formData.append('token', userToken)
  let username = null
  await request.post('/user/checkToken', formData).then(res => {
    if (res.code !== 200) {
      localStorage.removeItem('token')
      console.log('auth.js:登录状态(error token):' + false)
      return null
    }
    username = {
      uid: res.data.uid,
      username: res.data.username,
      avatar: res.data.avatar
    }
  }).catch(err => {
    localStorage.removeItem('token')
    console.log('auth.js:登录状态(catch error token):' + false)
    return null
  })
  console.log('auth.js:登录状态:' + true)
  console.log('auth.js:登录user:', username)
  return username
}

export { checkLogin }